package com.janal.service.login;

import com.janal.model.admin.Admin;
import com.janal.model.admin.AdminDao;
import com.janal.model.admin.AdminDaoImpl;
import com.janal.util.JdbcUtils;

import java.sql.Connection;
import java.sql.SQLException;

/**
 * @author Lenovo
 */
public class AdminLoginServiceImpl implements AdminLoginService{
    private AdminDao ad;

    public AdminLoginServiceImpl(){
        ad = new AdminDaoImpl();
    }

    public Admin adminLogin(Admin admin) {
        Connection conn = null;
        Admin resultAdmin = null;
        try {
            conn = JdbcUtils.getConnection();
            resultAdmin = ad.getLoginAdmin(conn, admin.getAdminCode());
            //若resultuser为空，说明用户不存在
            if(resultAdmin != null){
                if(!resultAdmin.getAdminPassword().equals(admin.getAdminPassword())){
                    //密码不正确，置resultuser的密码为空
                    resultAdmin.setAdminPassword(null);
                }
            }
        } catch (SQLException throwables) {
            throwables.printStackTrace();
        } catch (Exception e) {
            e.printStackTrace();
        }finally {
            JdbcUtils.close(null,conn);
        }
        return resultAdmin;
    }
}
